#!/bin/sh
# PCP QA Test No. 1069
# Exercise pmrep configuration and various reporting modes.
#
# Copyright (c) 2015-2021 Red Hat.
#

seq=`basename $0`
echo "QA output created by $seq"

. ./common.python

$python -c "from pcp import pmapi" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python pcp pmapi module not installed"
$python -c "from collections import OrderedDict" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python collections OrderedDict module not installed"

which pmrep >/dev/null 2>&1 || _notrun "pmrep not installed"

_need_metric kernel.all.cpu.wait

status=1	# failure is the default!
trap "cd $here; rm -rf $tmp.*; exit \$status" 0 1 2 3 15

_live_filter()
{
    sed \
	-e "s/[0-9][0-9]:[0-9][0-9]:[0-9][0-9]/HH:MM:SS/g" \
	-e '/derived/s/ from file.*:/ from file ...:/' \
	-e '/timezone/s/ (reporting.*)//' \
    #end
}

#  timezone: :Australia/Melbourne (reporting, current is AEDT-11)
#  this is not deterministic .... ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_archive_filter()
{
    sed \
	-e '/timezone/s/ (reporting.*)//' \
	-e "s#$here#QAPATH#g" \
    #end
}

_conf_error_filter()
{
    sed \
	-e "s/'\/.*'/CONF_FILE/g" \
	-e "s/errors: \/.*/errors: CONF_FILE/g" \
	-e "s/File contains/Source contains/g" \
	-e "s/: u'/: '/" \
    #end
}

log="--archive $here/archives/sample-secs -z"
log2="--archive $here/archives/20130706 -z -O 30m"

# real QA test starts here
echo "== basic live mode reporting"
pmrep -p -s 2 -t 0.25 sample.long.one | _live_filter
pmrep -p -T 2sec -t 1 sample.long.one | _live_filter
pmrep -p -T 2sec -t 0.8 sample.long.one | _live_filter
pmrep -p -T 1sec -t 1 -C sample.long.one sample.colour | _live_filter

echo "== basic duplicate name handling"
pmrep -C -U sample.seconds

echo "== basic PMNS/non-leaf metric handling"
pmrep -C -U kernel.all.cpu.wait kernel.all.cpu.wait.total

echo "== basic metric ordering"
pmrep -C -U kernel.all.cpu.wait mem.util.free

echo "== basic archive mode reporting"
pmrep -z -p -s 5 -t 2 -P 2 --archive $here/archives/sample-secs \
	sample.seconds sample.milliseconds

echo "== basic archive indom handling"
pmrep -a $here/archives/bozo-20170904 -z -C    tape.dev.io_ns mem.util.used
pmrep -a $here/archives/bozo-20170904 -z -C -I tape.dev.io_ns mem.util.used

echo "== basic configuration file handling"
cat > $tmp.config <<EOF
[options]
version = 1
source = www.example.com
samples = 1
interval = 1
EOF
pmrep -s 5 -t 2 $log -c $tmp.config sample

echo "== exercise various output options"
pmrep -s 3 -p -u $log2 hinv.machine
pmrep -s 5 -t 2 $log -o stdout -p -w 20 sample
echo "---"
pmrep -s 5 -t 2 $log -C -x sample | _archive_filter
echo "---"
pmrep -T 7s -t 3 -p $log -x sample | _archive_filter
echo "---"
pmrep -s 5 -t 2 $log -G -H sample
pmrep -s 5 -t 2 $log -G -U sample
pmrep -s 5 -t 2 $log2 -G --no-inst-info disk.dev.read
pmrep -s 5 -t 2 $log2 -G -U --no-inst-info disk.dev.read
pmrep -s 5 -t 2 $log2 -p -l '|' -X "" disk.dev.read disk.dev.write disk.dev.total network.interface.mtu mem.util.free kernel.all.load mem.util.used
pmrep -s 5 -t 2 $log2 -p -l '|' -X "Instance name" disk.dev.read disk.dev.write disk.dev.total network.interface.mtu mem.util.free kernel.all.load mem.util.used

echo "== exercise precision correctness"
pmrep -s 5 -t 2 $log -P 1 sample.seconds,,,,,,50
pmrep -s 5 -t 2 $log -0 3 sample.seconds,,,,,,50

echo "== exercise extended header"
pmrep -z -a $here/archives/rep -x -H -U -C -u -I "" | _archive_filter
pmrep -z -a $here/archives/20130706 -x -H -U -C -u -I "" | _archive_filter
pmrep -z -a $here/archives/bug-1044.meta -x -H -U -C -u -I "" | _archive_filter

echo "== exercise separate header"
pmrep -s 5 -t 2 $log2 -E 2 -g mem.util.free disk.dev.read mem.util.used
pmrep -s 5 -t 2 $log2 -p -l '|' -X "" -g mem.util.free disk.dev.read disk.dev.write mem.util.used
pmrep -s 5 -t 2 $log2 -p -X test -g mem.util.used kernel.all.cpu.user,%usr mem.util.free kernel.percpu.cpu.user,%usr mem.util.shared

echo "== invalid command line parameters"
pmrep -s 1 -t 1 -b GB -E 1x mem.util.free
pmrep -s 1 -t 1 -b GB -P 2x mem.util.free
pmrep -s 1 -t 1 -b GB -w 3x mem.util.free
pmrep -s 1 -t 1 -i '('      disk.dev.read 2>&1 | grep -o "Invalid regex '(':"
pmrep -s 1 -t 1 -i '.[]*'   disk.dev.read 2>&1 | grep -o "Invalid regex '.\[]\*':"
pmrep -s 1 -t 1 -i '.[z-a]' disk.dev.read 2>&1 | grep -o "Invalid regex '.\[z-a\]':"

echo "== metricset command line handling"
pmrep -z -a $here/archives/20201109 -i '.*000001.*' -C :nosuchset
pmrep -z -a $here/archives/20201109 -i '.*000001.*' -C :proc-cpu :nosuchset

echo "== invalid configuration file part 1"
cat > $tmp.wconfig <<EOF
[global]
mem.util.free = ,,
[test1]
output = x
[test2]
samples = -1
[test3]
precision = a
[test4]
space_scale = s
[test5]
incomplete = bogus
EOF
pmrep -C -c $tmp.wconfig -x :test1
pmrep -C -c $tmp.wconfig -x :test2 2>&1 | grep "sample count"
pmrep -C -c $tmp.wconfig -x :test3
pmrep -C -c $tmp.wconfig -x :test4
pmrep -C -c $tmp.wconfig -x :test5

echo "== invalid configuration file part 2"
cat > $tmp.wconfig <<EOF
[test1]
error
EOF
pmrep -C -c $tmp.wconfig -x :test1 2>&1 | _conf_error_filter

echo "== invalid configuration file part 3"
cat > $tmp.wconfig <<EOF
[test1]
error =
EOF
pmrep -C -c $tmp.wconfig -x :test1 2>&1 | _conf_error_filter

echo "== unsupported metric type"
pmrep -s 1 -t 1 sample.sysinfo

echo "== exercise non-integer options"
pmrep -T 2.5s -t 0.5 -p $log -x sample | _archive_filter

echo "== exercise raw counter mode"
pmrep --daemonize -s 5 -t 2 $log -r sample
pmsleep 1

echo "== exercise samples/interval/endtime settings and combinations"
# One at a time
pmrep -s 3 --archive $here/archives/sample-secs -p -x -z sample | _archive_filter
pmrep -t 3 --archive $here/archives/sample-secs -p -x -z sample | _archive_filter
pmrep -T 3 --archive $here/archives/sample-secs -p -x -z sample | _archive_filter
# Two at a time
pmrep -s 3 -t 3 --archive $here/archives/sample-secs -p -x -z sample | _archive_filter
pmrep -s 3 -T 3 --archive $here/archives/sample-secs -p -x -z sample | _archive_filter
pmrep -t 3 -T 9 --archive $here/archives/sample-secs -p -x -z sample | _archive_filter
# All at once
pmrep -s 3 -t 3 -T 1 --archive $here/archives/sample-secs -p -x -z sample | _archive_filter
pmrep -s 3 -t 3 -T 2 --archive $here/archives/sample-secs -p -x -z sample | _archive_filter
pmrep -s 3 -t 3 -T 3 --archive $here/archives/sample-secs -p -x -z sample | _archive_filter
pmrep -s 3 -t 3 -T 6 --archive $here/archives/sample-secs -p -x -z sample | _archive_filter
pmrep -s 2 -t 8 -T 4 --archive $here/archives/sample-secs -p -x -z sample | _archive_filter

echo "== exercise timezone setting"
pmrep -s 5 -t 2 --archive $here/archives/sample-secs -p -x -Z XYZ sample \
| _archive_filter
pmrep -s 5 -t 2 --archive $here/archives/sample-secs -p -x -Z UTC-0 sample \
| _archive_filter
pmrep -s 5 -t 2 --archive $here/archives/sample-secs -p -x -Z No/Such/Zone sample \
| _archive_filter
pmrep -s 5 -t 2 --archive $here/archives/sample-secs -p -x -Z UTC sample \
| _archive_filter
pmrep -s 5 -t 2 --archive $here/archives/sample-secs -p -x -Z UTC-5 sample \
| _archive_filter
pmrep -s 5 -t 2 --archive $here/archives/sample-secs -p -x -Z UTC+5 sample \
| _archive_filter
pmrep -s 5 -t 2 --archive $here/archives/sample-secs -p -x -Z America/New_York sample \
| _archive_filter
pmrep -s 5 -t 2 --archive $here/archives/sample-secs -p -x -Z :Europe/Helsinki sample \
| _archive_filter
pmrep -s 5 -t 2 --archive $here/archives/sample-secs -p -x -Z Asia/Kolkata sample \
| _archive_filter
pmrep -C -x -H -U -z -a $here/archives/numa hinv.machine | _archive_filter
pmrep -C -x -H -U -z -a $here/archives/rep hinv.machine | _archive_filter

echo "== exercise timezone change"
pmrep -u --archive $here/archives/coverage -p -x -z mem.util.free \
| _archive_filter
pmrep -u --archive $here/archives/coverage -p -x -Z UTC mem.util.free \
| _archive_filter

echo "== exercise starttime handling / confirm no negative duration/samples"
pmrep -C -x -H -U -Z UTC -a $here/archives/19970807.09.54 -S "@22:59:00" sample.milliseconds | _archive_filter

echo "== exercise extended CSV output"
pmrep -z -u -H -k -o csv -r -a $here/archives/bug1057 -I "" | wc -l
pmrep -s 5 -t 2 $log -o csv -r -k sample 2>&1 | _archive_filter

echo "== exercise delimiter corner cases"
pmrep -s 1 -t 1 $log2 -l '' -w 16 -U hinv.machine
echo '[testing]' > $tmp.dconfig
echo 'delimiter = " "' >> $tmp.dconfig
echo 'mem.util.free = freemem' >> $tmp.dconfig
echo 'mem.util.used = usedmem' >> $tmp.dconfig
pmrep -s 2 -t 2 $log2 -c $tmp.dconfig -p :testing | _archive_filter

echo "== exercise CSV and alternate delimiters"
pmrep -s 5 -t 2 $log -o csv -r -p -P 1 sample
pmrep -s 5 -t 2 $log -o csv -r --delimiter '|' sample | \
    tee -a $tmp.out1
pmrep -s 5 -t 2 $log -o csv -r -k -l ';' sample 2>&1 | _archive_filter

echo "== exercise CSV and instance handling"
pmrep -s 5 -t 2 $log2 -o csv -r hinv.map.scsi kernel.all.load
echo "== compare output-to-file versus stdout"
pmrep -s 5 -t 2 $log -o csv -r --delimiter '|' -F $tmp.out2 sample
diff $tmp.out1 $tmp.out2
test $? -eq 0 && echo ok

echo "== exercise repeated headers option"
pmrep -s 5 -t 2 $log -E 2 sample.seconds

echo "== exercise units conversion options"
pmrep -s 5 -t 2 $log2 disk.dev.read,,sda,1
pmrep -s 5 -t 2 $log2 -w 15 -P 1 -p -b MB mem.util.free mem.util.used
pmrep -s 5 -t 2 $log2 -w 15 -P 2 -p -b GB mem.util.free mem.util.used
pmrep -s 5 -t 2 $log2 -w 15 -P 3 -p -q "count x 10^2" disk.dev.read
pmrep -s 5 -t 2 $log2 -w 15 -P 4 -p -q "count x 10^-2" disk.dev.read
pmrep -s 5 -t 2 $log2 -w 15 -P 5 -p -y sec -u disk.dev.avactive
pmrep -s 5 -t 2 $log2 -w 15 -P 6 -p -y microsec -u disk.dev.avactive
pmrep -s 5 -t 2 $log2 -w 15 -P 7 -p -q "count x 10^6" -r kernel.all.sysfork
pmrep -s 5 -t 2 $log2 -w 15 -P 8 -p -b kB mem.util.free mem.util.used
pmrep -s 5 -t 2 $log2 -w 15 -P 9 -p -b MB/s mem.util.free mem.util.used
pmrep -s 5 -t 2 $log2 -w 15 -P 8 -p mem.util.free,,,kb/s mem.util.used,,,GB/ms
pmrep -s 5 -t 2 -z -a $here/archives/rep -u -p mem.util.used,,,GB/ms network.interface.speed,,,GB/hr

echo "== exercise compact metricset specification and overrides"
pmrep -s 5 -t 2 $log2 -w 128 -P 1 -p -b kB "mem.util.free,mem free,,MB,,15,0" mem.util.used,mem-used,,GB,,15,2

echo "== exercise forced override options"
pmrep -s 1 -t 2 $log2 -w 15 mem.util.used,,,MB,,
pmrep -s 1 -t 2 $log2 mem.util.used,,,MB,,15
pmrep -s 1 -t 2 $log2 -w 25 mem.util.used,,,MB,,15
pmrep -s 1 -t 2 $log2 -W 15 mem.util.used,,,MB,,25
pmrep -s 2 -t 2 $log2 -P 1 disk.dev.read,,sda,,,,
pmrep -s 2 -t 2 $log2 disk.dev.read,,sda,,,,1
pmrep -s 2 -t 2 $log2 -P 9 disk.dev.read,,sda,,,,1
pmrep -s 2 -t 2 $log2 -0 1 disk.dev.read,,sda,,,,9
pmrep -s 2 -t 2 $log2 disk.dev.read,,sda,,raw
pmrep -s 2 -t 2 $log2 -r disk.dev.read,,sda,,
pmrep -s 2 -t 2 $log2 -r disk.dev.read,,sda,,default
pmrep -s 2 -t 2 $log2 -R disk.dev.read,,sda,,
pmrep -s 2 -t 2 $log2 -R disk.dev.read,,sda,,default
pmrep -s 1 -t 2 $log2 -b GB mem.util.used,,,MB
pmrep -s 1 -t 2 $log2 -B MB mem.util.used,,,GB

echo "== derived metrics configuration file"
pmrep -e $tmp.nosuchfile metric 2>&1 | _live_filter
cat > $tmp.derived <<EOF
sample.combo = sample.seconds + (sample.milliseconds / 1000)
EOF
pmrep -s 5 -t 2 $log -e $tmp.derived sample.combo

echo "== derived metrics directly on command line"
combo="sample.combo = sample.seconds + (sample.milliseconds / 1000)"
pmrep -s 5 -t 2 $log -e "$combo" sample.combo
pmrep -C -H -U -e 'derived_a = delta(disk.all.read_bytes) + delta(disk.all.write_bytes);derived_b = delta(disk.all.read_bytes) + delta(disk.all.write_bytes) == 0 ? mkconst(0, type="double", semantics="instant") : 100 * (delta(proc.io.read_bytes) + delta(proc.io.write_bytes) - delta(proc.io.cancelled_write_bytes)) / (delta(disk.all.read_bytes) + delta(disk.all.write_bytes))' derived_a derived_b
pmrep -C -H -U -e 'derived_a = delta(disk.all.read_bytes) + delta(disk.all.write_bytes)' -e 'derived_b = delta(disk.all.read_bytes) + delta(disk.all.write_bytes) == 0 ? mkconst(0, type="double", semantics="instant") : 100 * (delta(proc.io.read_bytes) + delta(proc.io.write_bytes) - delta(proc.io.cancelled_write_bytes)) / (delta(disk.all.read_bytes) + delta(disk.all.write_bytes))' derived_a derived_b

echo "== derived metrics definition files on command line"
cat > $tmp.derived.conf1 <<EOF
derived_a = delta(disk.all.read_bytes) + delta(disk.all.write_bytes)
EOF
cat > $tmp.derived.conf2 <<EOF
derived_b = delta(proc.io.read_bytes) + delta(proc.io.write_bytes)
EOF
pmrep -C -H -U -e $tmp.derived.conf1 -e $tmp.derived.conf2 derived_a derived_b
pmrep -C -H -U -e $tmp.derived.conf1\;$tmp.derived.conf2 derived_a derived_b
echo "No output expected."

echo "== derived metrics in pmrep.conf"
cat > $tmp.derived-test.conf <<EOF
[derived-test]
derived = derived_a = delta(disk.all.read_bytes) + delta(disk.all.write_bytes),derived_b = delta(proc.io.read_bytes) + delta(proc.io.write_bytes)
derived_a = ,,
derived_b = ,,
EOF
pmrep -C -H -U -a $here/archives/proc -c $tmp.derived-test.conf :derived-test
echo "No output expected."
cat > $tmp.derived-test.conf <<EOF
[derived-test]
iopct = p.i.io_pct
iopct.formula = delta(disk.all.read_bytes) + delta(disk.all.write_bytes) == 0 ? mkconst(0, type="double", semantics="instant") : 100 * (delta(proc.io.read_bytes) + delta(proc.io.write_bytes) - delta(proc.io.cancelled_write_bytes)) / (delta(disk.all.read_bytes) + delta(disk.all.write_bytes))
EOF
pmrep -C -H -U -a $here/archives/proc -c $tmp.derived-test.conf :derived-test
echo "No output expected."
cat > $tmp.derived-test.conf <<EOF
[derived-test]
derived = derived_a = delta(disk.all.read_bytes) + delta(disk.all.write_bytes);derived_b = delta(disk.all.read_bytes) + delta(disk.all.write_bytes) == 0 ? mkconst(0, type="double", semantics="instant") : 100 * (delta(proc.io.read_bytes) + delta(proc.io.write_bytes) - delta(proc.io.cancelled_write_bytes)) / (delta(disk.all.read_bytes) + delta(disk.all.write_bytes))
derived_a = ,,
derived_b = ,,
EOF
pmrep -C -H -U -a $here/archives/proc -c $tmp.derived-test.conf :derived-test
echo "No output expected."

echo "== derived metrics v. instance profiles"  # GH#451, GH#1253
# GH#451  - first column should have same values
pmrep -s 1 -w 8 -H -U -e 'y = sum(sample.bucket)' y sample.bucket
pmrep -s 1 -w 8 -H -U -e 'y = sum(sample.bucket)' y
# GH#1253 - should have values for all instances
pmrep -s 1 -w 8 -H -U -e 'y = kernel.all.uptime-proc.psinfo.start_time' -a $here/archives/20201109 -z -O 30m -i 1,2,3 y

echo "== extended existing sample configuration"
cat > $tmp.dconfig <<EOF
[global]
combo = sample.combo
EOF
pmrep -s 5 -t 2 $log -e $tmp.derived -c $tmp.dconfig sample

echo "== exercise good config version"
echo '[options]' > $tmp.vconfig
echo 'version = 1' >> $tmp.vconfig
pmrep -s 5 -t 2 $log -c $tmp.vconfig sample
echo "== exercise bad config version"
echo '[options]' > $tmp.vconfig
echo 'version = 99' >> $tmp.vconfig
pmrep -s 5 -t 2 $log -c $tmp.vconfig sample

echo "== un/interpolated archive mode reporting"
pmrep -s 5 -t 2 $log2 -p    mem.util.free
pmrep -s 5 -t 2 $log2 -p -u mem.util.free

echo "== un/interpolated archive mode reporting with endtime"
pmrep -t 20 $log2 -p    -T @00:50:00 mem.util.free
pmrep       $log2 -p -u -T @00:50:00 mem.util.free

echo "== time alignment with archive/host"
pmrep -s 5 -t 2 $log2 -p -A 2m mem.util.free
pmrep -s 2 -t 2 -p -A 2s -l X sample.long.one \
| sed -r -e 's,[0-9][0-9]:[0-9][0-9]:[0-9][02468]X,ALIGN_OK_X,g'

echo "== basic string value reporting"
# sets variable named $pcp_version
eval `pmprobe -v pmcd.version | sed -e 's/ 1 /=/g' -e 's/pmcd./pcp_/g'`
pmrep -s 1 pmcd.version | sed -e "s/  *$pcp_version/PCP_VERSION/g"

echo "== divide-by-zero error handling"
pmrep -t .01 -s 1 -e 'fail = sample.long.one / 0' fail

echo "== test ignoring and omitting metrics"
pmrep -C $log2 -u -U -q MB    mem.util
pmrep -C $log2 -u -U -q MB -I mem.util
pmrep -C $log2 -u    -i sda mem.util.free disk.dev mem.util
pmrep -C $log2 -u -v -i sda mem.util.free disk.dev mem.util

echo "== output value truncation"
pmrep -s 1 $log2 -w 3 hinv.machine
pmrep -s 1 $log2 -w 3 mem.util.free

echo "== handle empty/invalid/multiple metricsets in config file"
echo '[fail]' > $tmp.econfig
echo 'failure = yes' >> $tmp.econfig
pmrep -s 1 -x -C -H -U $log2 -c $tmp.econfig :fail
echo '[options]' > $tmp.econfig
echo 'mem.util.free = freemem' >> $tmp.econfig
pmrep -s 1 -x -C -H -U $log2 -c $tmp.econfig hinv.machine 2>&1 | _archive_filter | _conf_error_filter
echo '[empty]' > $tmp.econfig
echo 'interval = 2s' >> $tmp.econfig
echo '[metric]' >> $tmp.econfig
echo 'mem.util.free = freemem' >> $tmp.econfig
echo 'interval = 3s' >> $tmp.econfig
pmrep -s 1 -x -C -H -U $log2 -c $tmp.econfig :empty | _archive_filter
pmrep -s 1 -x -C -H -U $log2 -c $tmp.econfig :metric | _archive_filter
pmrep -s 1 -x -C -H -U $log2 -c $tmp.econfig :empty :metric | _archive_filter
pmrep -s 1 -x -C -H -U $log2 -c $tmp.econfig :metric :empty | _archive_filter

echo "== handle options/global/metricset section corner cases / issues"
echo '[options]' > $tmp.cconfig
echo 'interval = 2s' >> $tmp.cconfig
echo 'mem.util.free = freemem' >> $tmp.cconfig
# No metrics in options allowed
pmrep -s 1 -x -C $log2 -c $tmp.cconfig mem.util.used 2>&1 | _archive_filter | _conf_error_filter
echo '[testing]' > $tmp.cconfig
echo 'colxrow = " test "' >> $tmp.cconfig
echo 'mem.util.free = freemem' >> $tmp.cconfig
# colxrow quote handling
pmrep -s 1 -C $log2 -c $tmp.cconfig :testing | _archive_filter
echo '[testing]' > $tmp.cconfig
echo 'colxrow = yes' >> $tmp.cconfig
echo 'mem.util.free = freemem' >> $tmp.cconfig
# colxrow boolean value handling
pmrep -s 1 -C $log2 -c $tmp.cconfig :testing | _archive_filter
echo '[options]' > $tmp.cconfig
echo 'interval = 2s' >> $tmp.cconfig
echo '[global]' >> $tmp.cconfig
echo 'interval = 3s' >> $tmp.cconfig
echo 'mem.util.free = freemem' >> $tmp.cconfig
# No options in globals allowed
pmrep -s 1 -x -C $log2 -c $tmp.cconfig -G mem.util.used 2>&1 | _archive_filter | _conf_error_filter
pmrep -s 1 -x -C $log2 -c $tmp.cconfig    mem.util.used 2>&1 | _archive_filter | _conf_error_filter
echo '[options]' > $tmp.cconfig
echo 'interval = 2s' >> $tmp.cconfig
echo '[global]' >> $tmp.cconfig
echo 'mem.util.free = freemem' >> $tmp.cconfig
echo '[testing]' >> $tmp.cconfig
echo 'mem.util.used = usedmem' >> $tmp.cconfig
# No metrics
pmrep -s 1 -x -C $log2 -c $tmp.cconfig -G :options | _archive_filter
# Ok to pass
pmrep -s 1 -x -C $log2 -c $tmp.cconfig    :global | _archive_filter
pmrep -s 1 -x -C $log2 -c $tmp.cconfig -G :global :testing | _archive_filter
# Expected to pass
pmrep -s 1 -x -C $log2 -c $tmp.cconfig -G :testing | _archive_filter
pmrep -s 1 -x -C $log2 -c $tmp.cconfig    :testing | _archive_filter

echo "== handle same labels"
echo '[l]' > $tmp.lconfig
echo 'kernel.all.cpu.user = %%usr' >> $tmp.lconfig
echo 'kernel.percpu.cpu.user = %%usr' >> $tmp.lconfig
pmrep -s 2 -r -u -w 12 $log2 -c $tmp.lconfig         :l
pmrep -s 2 -r -u -w 12 $log2 -c $tmp.lconfig -X test :l

echo "== exercise option priority"
echo '[options]' > $tmp.pconfig
echo 'header = yes' >> $tmp.pconfig
echo 'unitinfo = no' >> $tmp.pconfig
echo 'interval = 2s' >> $tmp.pconfig
# Use built-in defaults for header/unitinfo/interval
pmrep -s 5 $log2 -p mem.util.free
echo "---"
# Override built-in defaults of unitinfo/interval
pmrep -s 5 $log2 -p -c $tmp.pconfig mem.util.free
echo "---"
# Override built-in defaults / config of header/interval
pmrep -s 5 $log2 -t 3s -p -H -c $tmp.pconfig mem.util.free
echo "---"
echo '[options]' > $tmp.pconfig
echo "source = $here/archives/no-such-archive" >> $tmp.pconfig
echo '[source]' >> $tmp.pconfig
echo "source = $here/archives/rep" >> $tmp.pconfig
echo 'kernel.all.uptime = uptime,,,,8' >> $tmp.pconfig
# Override generic option with metricset specific option
pmrep -s 5 -c $tmp.pconfig -z -p -u :source
echo "---"
echo '[source]' > $tmp.pconfig
echo 'source = wrong.example.com' >> $tmp.pconfig
echo 'kernel.all.uptime = uptime,,,,8' >> $tmp.pconfig
# Override config file source with command line option
pmrep -s 5 $log2 -c $tmp.pconfig -p -u :source
echo "---"
echo '[source]' > $tmp.pconfig
echo 'source = @' >> $tmp.pconfig
echo "speclocal = clear;add,29,sample/pmda_sample.$DSO_SUFFIX,sample_init" >> $tmp.pconfig
echo 'sample.long.one = one,,,,8' >> $tmp.pconfig
# Test speclocal from config file
pmrep -s 1 -c $tmp.pconfig -p -u :source | _live_filter
echo "---"
echo '[source]' > $tmp.pconfig
echo 'source = @' >> $tmp.pconfig
echo 'speclocal = fail' >> $tmp.pconfig
echo 'sample.long.one = one,,,,8' >> $tmp.pconfig
# Test speclocal override from command line
pmrep -s 1 -K clear -K add,29,sample/pmda_sample.$DSO_SUFFIX,sample_init -c $tmp.pconfig -p -u :source | _live_filter
echo "---"
echo '[options]' > $tmp.pconfig
echo 'interval = 4s' >> $tmp.pconfig
echo '[testing]' >> $tmp.pconfig
echo 'unitinfo = yes' >> $tmp.pconfig
echo 'timestamp = no' >> $tmp.pconfig
echo 'interval = 3s' >> $tmp.pconfig
echo 'mem.util.free = freemem' >> $tmp.pconfig
# Test overriding metricset specific settings (#357)
pmrep -s 3 $log2 -p -c $tmp.pconfig -U -t 2s :testing

echo "== exercise signal handling"
pmrep -T 6sec -t 3 sample.long.one > $tmp.bgout 2>&1 &
pid=$!
pmsleep 4
$PCP_BINADM_DIR/pmsignal -s TERM $pid
cat $tmp.bgout

# success, all done
echo "== done"
status=0
exit
